package top.lunarye.handler.deduplication;

import org.springframework.stereotype.Service;
import top.lunarye.common.domaim.TaskInfo;
import top.lunarye.common.enums.DeduplicationType;

import javax.annotation.Resource;
import java.util.List;

/**
 * 去重服务
 * @author lunar
 * @since 2022-07-19
 */
@Service
public class DeduplicationRuleService {

    // 配置默认的去重规则
    // 每5分钟只能有一条重复消息
    // 每天单一用户单一渠道只能发送五条消息
    private final String DEDUPLICATION_RULE = "{\"deduplication_10\":{\"num\":1,\"time\":300},\"deduplication_20\":{\"num\":5}}";

    @Resource
    private DeduplicationHolder deduplicationHolder;

    public void duplication(TaskInfo taskInfo) {

        List<Integer> deduplicationList = DeduplicationType.getDeduplicationList();

        for (Integer deduplicationType : deduplicationList) {
            DeduplicationParam deduplicationParam = deduplicationHolder.selectBuilder(deduplicationType)
                    .build(DEDUPLICATION_RULE, taskInfo);
            if (deduplicationParam != null) {
                deduplicationHolder.selectService(deduplicationType).deduplication(deduplicationParam);
            }
        }
    }

}
